<?php
class Zendvn_System_Auth {
	
	protected $_mesageError;
	public function login($arrParam,$options = null){
		$db = Zend_Registry::get('connectDB');
		if($arrParam['email']!=null){
			$authAdapter = new Zend_Auth_Adapter_DbTable($db, 'users','email','password');
		
		    $select = $authAdapter->getDbSelect();
		    $select->where('status = 1');
		    $email = $arrParam['email'];
		    $pass = new Zendvn_Encode();
		    $password = $pass->password($arrParam['password']);
		
		    $authAdapter->setIdentity($email);
		    $authAdapter->setCredential($password);
		
		   //2.Khoi tao Zend Auth
		    $auth = Zend_Auth::getInstance();
		    $result = $auth->authenticate($authAdapter);
		    $flagAccess = false;
		   if(!$result->isValid()){
		 	    $msg = $result->getMessages();
			    $this->_mesageError = current($msg);
		    }else{
			
			    $returnColumns = null;
			    $omitColumns = array('password','active_code','status');
			    $data = $authAdapter->getResultRowObject($returnColumns, $omitColumns);
			
			    $auth->getStorage()->write($data);
			    $flagAccess = true;
		}
		return $flagAccess;
		}else{
			$this->_mesageError = 'Vui lòng nhập email và password';
		}
		
	}
	
	public function getError(){
		return $this->_mesageError;
	}
	
	public function logout($arrParam = null,$options = null){
		$auth = Zend_Auth::getInstance();
		$auth->clearIdentity();
	}
}